<?php


namespace App\Http\Controllers;

use App\Models\ApiResult;
use Illuminate\Support\Facades\Validator;
class ApiBaseController extends Controller
{

    public function success($data = null, $message = "操作成功")
    {
        return $this->responseJson(ApiResult::success($data, $message));
    }

    public function failure($code = 500, $message = "操作失败", $data = null)
    {
        return $this->responseJson(ApiResult::failure($code, $message, $data));
    }

    protected function responseJson($data)
    {
        return response()->json($data, 200, [], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }


    public function verifyField($input, $rules)
    {
        $errors = Validator::make($input, $rules)->errors();
        if (!blank($errors->first())) return response()->json(['code' => 500, 'msg' => $errors->first()]);
    }
}



